package com.leeh.jsontest;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Liheng on 2017-04-14.
 */

public class DataParse {
    public static void parseHtml(String html) {
        Document doc = Jsoup.parse(html, "gbk");
        Elements links = doc.getElementsByTag("a");
        for (Element link : links) {
            String linkClick = link.attr("onclick");
            String linkHref = link.attr("href");
            String linkText = link.text();
            Log.i(linkText, linkHref + linkClick);
        }
    }

    /**
     * 解析option 被包含在script里面
     *
     * @param html
     * @return
     */
    public static List<SpinnerContent> parseScriptOption(String html, String name) {
        Document doc = Jsoup.parse(html, "gbk");
        Elements links = doc.getElementsByTag("script");
        return parseHTMLOption(links.html(), name);
    }

    /**
     * 选择需要的解析option内容
     *
     * @param html
     * @param name
     * @return
     */
    private static String selectNeedHTML(String html, String name) {
        if (name == null || name.equals("")) {
            return html;
        }
        Document doc = Jsoup.parse(html, "gbk");
        Elements links = doc.getElementsByTag("select");
        for (Element element : links) {
            String selectContent = element.attr("name");
            if (selectContent.equals(name)) {
                return element.html();
            }
        }
        return "";
    }

    /**
     * 解析option
     *
     * @param html
     * @return
     */
    public static List<SpinnerContent> parseHTMLOption(String html, String name) {
        html = selectNeedHTML(html, name);
        Document doc = Jsoup.parse(html, "gbk");
        Elements links = doc.getElementsByTag("option");
        List<SpinnerContent> spinnerContents = new ArrayList<SpinnerContent>();
        for (Element element : links) {
            spinnerContents.add(new SpinnerContent(element.attr("value"), element.text()));
        }
        return spinnerContents;
    }

    /**
     * 检验验证码
     *
     * @param html
     * @return
     */
    public static boolean parseCheckVerCode(String html) {
        if (html == null) {
            return false;
        }
        html = parseJsonToCourse(html);
        if (html.equals("null") || html.equals("txt_yzm")){
            return false;
        }
        return true;
    }

    /**
     * 解析课表
     *
     * @param html
     * @return
     */
    public static List<String> parseCourse(String html) {
        html = parseJsonToCourse(html);
        List<String> contents = new ArrayList<String>();
        Document doc = Jsoup.parse(html, "gbk");
        Elements links = doc.select("tbody");
        for (Element element : links) {
            doc = Jsoup.parse("<table>" + element.html() + "</table>", "gbk");
            Elements elsTr = doc.select("tr");
            for (int i = 0; i < elsTr.size(); i++) {
                doc = Jsoup.parse("<table>" + elsTr.get(i).html() + "</table>", "gbk");
                Elements elsTd = doc.select("td");
                for (int j = 0; j < elsTd.size(); j++) {
                    String content = elsTd.get(j).text();
                    contents.add(content);
                }
            }
        }
        return contents;
    }

    private static String parseJsonToCourse(String json){
        String html = "";
        try {
            JSONObject jsonObject = new JSONObject(json);
            html = jsonObject.getString("schedule_content");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return html;
    }

    /**
     * 解析下拉列表json
     * @param json
     * @param className 类明
     * @return
     */
    public static List<SpinnerContent> parseSpinnerFromJsonToList(String json, String className) {
        List<SpinnerContent> spinnerContents = new ArrayList<SpinnerContent>();
        try {
            JSONArray jsonArray=new JSONArray(json);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                spinnerContents.add(new SpinnerContent(jsonObject.getString(className + "_id"),
                        jsonObject.getString(className + "_name")));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        System.out.println("parseSpinnerFromJsonToList");
        return spinnerContents;
    }

}
